package com.mall.goods.service.impl;

import com.mall.goods.model.SkuAttribute;
import com.mall.goods.mapper.SkuAttributeMapper;
import com.mall.goods.service.ISkuAttributeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * 商品属性表 服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-08-18
 */
@Service
public class SkuAttributeServiceImpl extends ServiceImpl<SkuAttributeMapper, SkuAttribute> implements ISkuAttributeService {
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addSkuAttribute(SkuAttribute skuattribute){
        return super.save(skuattribute);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteById(Integer id){
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateSkuAttribute(SkuAttribute skuattribute){
        return super.updateById(skuattribute);
    }

    @Override
    public SkuAttribute getById(Integer id){
        return super.getById(id);
    }

    @Override
    public List<SkuAttribute> selectList(SkuAttribute skuattribute){
        return super.list(getQueryWrapper(skuattribute));
    }

    @Override
    public Page<SkuAttribute> selectPage(Page<SkuAttribute> page, SkuAttribute skuattribute){
        return super.page(page, getQueryWrapper(skuattribute));
    }

    public LambdaQueryWrapper<SkuAttribute> getQueryWrapper(SkuAttribute skuattribute){
        return Wrappers.<SkuAttribute>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(skuattribute.getId()), SkuAttribute::getId, skuattribute.getId())
            .eq(ObjectUtils.isNotEmpty(skuattribute.getName()), SkuAttribute::getName, skuattribute.getName())
            .eq(ObjectUtils.isNotEmpty(skuattribute.getOptions()), SkuAttribute::getOptions, skuattribute.getOptions())
            .eq(ObjectUtils.isNotEmpty(skuattribute.getSort()), SkuAttribute::getSort, skuattribute.getSort())
        ;
    }
}
